package com.factory._17memento;

import com.factory._17memento.demo1.Caretaker;
import com.factory._17memento.demo1.Originator;
import com.factory._17memento.demo2.Caretaker2;
import com.factory._17memento.demo2.Originator2;

/**
 * 备忘录模式
 * @author Qinye
 * @date 2023/8/13 18:22
 */
public class Test {
    public static void main(String[] args) {
        // demo1 白箱设计
        //创建发起人
        Originator originator = new Originator();
        //创建负责人
        Caretaker caretaker = new Caretaker();
        //改变负责人对象的状态
        originator.setState("On");
        caretaker.setMemento(originator.createMemento());

        originator.setState("off");
        originator.restoreMemento(caretaker.getMemento());
        System.out.println(originator.getState());
        System.out.println("--------------");
        //demo2 黑箱设计 负责人完全无法修改备忘录的内容
        Originator2 originator2 = new Originator2();
        Caretaker2 caretaker2 = new Caretaker2();
        originator2.setState("On");
        caretaker2.setMemento(originator2.createMemento());
        originator2.setState("Off");
        originator2.restoreMemento(caretaker2.getMemento());
        System.out.println(originator2.getState());
    }
}
